package com.testmall.controller;


import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.testmall.entity.User;
import com.testmall.entity.common.ResultBean;
import com.testmall.entity.common.TokenProperties;
import com.testmall.service.UserService;
import com.testmall.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: littleDesire
 * @Date: 2022-06-21 14:30
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/user")
@ApiSort(value = 1)

@Api(value = "用户接口", tags = "用户接口")
public class UserController extends BaseController{
    @Autowired
    UserService userService;
    @Autowired
    TokenProperties tokenProperties;
    @Autowired
    TokenUtil tokenUtil;

    /**
     * 用户注册
     * @param user 用户对象
     * @return 注册信息
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册", notes = "注册用户")
    public String registerUser(User user) {
        int rs = userService.registerUser(user);
        return rs > 0 ? "注册成功" : "注册失败：用户名已存在或手机号已被注册";
    }

    /**
     * 用户登录
     * @param username  用户名
     * @param password  密码
     * @return  登录信息
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "用户登录")
    public String login(HttpServletRequest request, @RequestParam(value = "username", required = false) String username, @RequestParam(value = "phone", required = false)String phone, @RequestParam(value = "password", required = true) String password) {
        Map<String, Object> map = new HashMap<>();
        if (username != null){
            map.put("username", username);
            request.getSession().setAttribute("username",username);
        }
        if (phone != null) {
            map.put("phone", phone);
            request.getSession().setAttribute("phone",phone);
        }
        map.put("password", password);
        int rs = userService.login(map);
        if (rs > 0) {
            User.builder().token(tokenUtil.generateToken(new HashMap<>()));
        }
        return rs > 0 ? "登录成功" : "登录失败";
    }

    /**
     * 生成token
     * @return token
     */
    @GetMapping("/token")
    @ApiOperation(value = "Token", notes = "Token生成")
    public Map<String, Object> token() {
        String s = tokenUtil.generateToken(new HashMap<>());
        Map<String, Object> map = new HashMap<>();
        map.put("token", s);
        boolean enable = tokenProperties.getEnable();
        List<String> excludePathPatterns = tokenProperties.getExcludePathPatterns();
        List<String> pathPatterns = tokenProperties.getPathPatterns();
        String secret = tokenProperties.getSecret();
        int validateTime = tokenProperties.getValidateTime();
        map.put("excludePathPatterns", excludePathPatterns);
        map.put("pathPatterns", pathPatterns);
        map.put("validateTime", validateTime);
        map.put("enable", enable);
        map.put("secret", secret);
        return map;
    }

    @PostMapping("/changePassword")
    @ApiOperation(value = "修改密码", notes = "需要手机号，密码，")
    public String changePassword(String oldPassword , String newPassword, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        if (session.getAttribute("username") != null){
            map.put("username", session.getAttribute("username"));
        }
        if (session.getAttribute("phone") != null){
            map.put("phone", session.getAttribute("phone"));
        }
        map.put("oldPassword", oldPassword);
        map.put("newPassword", newPassword);
        int rs = userService.changePassword(map);
        return rs > 0 ? "修改成功" : "修改失败";
    }

    @PostMapping("/queryUser")
    @ApiOperation(value = "查询所有用户", notes = "无条件查询所有用户信息")
    public ResultBean queryUser() {
        List<Map<String, Object>> users = userService.queryUser();
        return success("所有用户数据", users);
    }
}
